package ws;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.util.Log;

public class LibroWS {
	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;

	/*
	 * 
	 */
	public void coneccionws(){

		boolean londres = true;

		if(londres){
			NAMESPACE = "http://ws/"; 
			URL = "http://172.22.4.108:8080/Libro2SOA/services/LibroSOA?wsdl";
			

			//	Toast.makeText(null, "servidor", Toast.LENGTH_SHORT).show();
			//		Toast.makeText(getBaseContext(),respuesta, 
			//				Toast.LENGTH_SHORT).show();
		}else{
			NAMESPACE = "http://ws/";
			URL = "http://172.22.4.108:8080/Libro2SOA/services/LibroSOA?wsdl";
		}
	}

	public int autentificarLibro(String clave){
		coneccionws();

		METHOD_NAME = "autentificarlibro";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findlibro1 = new PropertyInfo();

		findlibro1.setName("clave");
		findlibro1.setValue(clave);
		findlibro1.setType(String.class);

		request.addProperty(findlibro1);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);

        int num=0;
		String idLibro;
		try
		{

			aht.call(SOAP_ACTION, soapEnvelope);		
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			idLibro =resultString.toString();
			Log.i("log-android","ha llegado-- web autentificar***--"+idLibro);
            num= Integer.parseInt(idLibro);
			//return num;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			num = 0;}
		return num;
	}
	
	public String crearGrafico(int idLibro,int idPregunta){
		coneccionws();

		METHOD_NAME = "creargrafico";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo libro = new PropertyInfo();
		PropertyInfo pregunta = new PropertyInfo();

		libro.setName("idLibro");
		libro.setValue(idLibro);
		libro.setType(int.class);

		pregunta.setName("idPregunta");
		pregunta.setValue(idPregunta);
		pregunta.setType(int.class);

		request.addProperty(libro);
		request.addProperty(pregunta);
		//Log.i("log-android","ha llegado aqui ws--"+findempresa);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String respuesta;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			Log.i("log-android","ha llegado aquiiii probando--"+URL);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

			respuesta =resultString.toString();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			respuesta = "Error Conexion";}
		return respuesta;


	}

}

